/**
 * 添加或者修改页面
 */
var FlowStoreCityInfoDlg = {
    data: {
        storeCityId: "",
        storeName: "",
        longitude: "",
        latitude: "",
        province: "",
        city: "",
        address: "",
        isDetault: "",
        direct:"",
        discount:"",
        oneAuthorityDiscount:"",
        oneAuthorityCityDiscount:""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;























    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/flowStoreCity/addItem", function (data) {
            Feng.success("添加成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    var map = new AMap.Map("l-map", {
        resizeEnable: true,
        zoom: 13
    });
    var marker = undefined;
    var geocoder = new AMap.Geocoder({

    });
    function showInfoClick(e){
        if(marker){
            marker.setMap(null);
            marker = undefined;
        }
        marker = new AMap.Marker({
            position: [e.lnglat.getLng(),e.lnglat.getLat()]
        });
        $("#longitude").val(e.lnglat.getLng());
        $("#latitude").val(e.lnglat.getLat());
        marker.setMap(map);
        //获取位置
        var lnglat = [e.lnglat.getLng(),e.lnglat.getLat()];

        geocoder.getAddress(lnglat, function(status, result) {
            if (status === 'complete'&&result.regeocode) {
                if(result.regeocode.addressComponent){
                    var province = result.regeocode.addressComponent.province;
                    var city = result.regeocode.addressComponent.city;
                    $("#province").val(province);
                    $("#city").val(city);
                }

                var address = result.regeocode.formattedAddress;
                document.getElementById('address').value = address;
            }else{
                log.error('根据经纬度查询地址失败')
            }
        });
    }
    map.on('click', showInfoClick);
    //地图检索
    var autoOptions = {
        input: "suggestId"
    };
    var auto = new AMap.Autocomplete(autoOptions);
    var placeSearch = new AMap.PlaceSearch({
        map: map
    });  //构造地点查询类
    AMap.event.addListener(auto, "select", select);//注册监听，当选中某条记录时会触发
    function select(e) {
        placeSearch.setCity(e.poi.adcode);
        placeSearch.search(e.poi.name);  //关键字查询查询
    }
});